<script setup lang="ts">
import type { CreateReadingQuestion } from '@/types'
import { AIWrite, Plus } from '@/components/icons'

interface Props {
  questions: CreateReadingQuestion[]
  showCreateButton?: boolean
  showAIButton?: boolean
}

interface Emits {
  (e: 'createQuestion'): void
  (e: 'editQuestion', question: CreateReadingQuestion): void
  (e: 'removeQuestion', index: number): void
  (e: 'moveQuestionUp', index: number): void
  (e: 'moveQuestionDown', index: number): void
  (e: 'aiGenerate'): void
}

withDefaults(defineProps<Props>(), {
  showCreateButton: true,
  showAIButton: true,
})

defineEmits<Emits>()
</script>

<template>
  <div class="question-list-card card-area">
    <div class="question-list-card__header">
      <div class="question-list-card__title">
        题目列表
      </div>
      <div class="question-list-card__actions">
        <button v-if="showCreateButton" class="question-list-card__action-btn" @click="$emit('createQuestion')">
          <Plus />
          新建题目
        </button>
        <button v-if="showAIButton" class="question-list-card__action-btn" @click="$emit('aiGenerate')">
          <AIWrite />
          <span style="width: 4rem;">AI生题</span>
        </button>
      </div>
    </div>

    <div class="question-list-card__content">
      <div v-if="questions.length === 0" class="question-list-card__empty">
        <div class="question-list-card__empty-icon">
          📝
        </div>
        <div class="question-list-card__empty-text">
          暂无题目
        </div>
      </div>

      <div v-for="(question, index) in questions" :key="index" class="question-list-card__item">
        <div class="question-list-card__item-header">
          <div class="question-list-card__item-number">
            {{ index + 1 }}
          </div>
          <div
            class="question-list-card__item-type"
            :class="question.type === 0 ? 'question-list-card__item-type--choice' : 'question-list-card__item-type--essay'"
          >
            {{ question.type === 0 ? '选择题' : '简答题' }}
          </div>
          <div class="question-list-card__item-actions">
            <button v-if="index !== 0" class="question-list-card__item-action-btn question-list-card__item-action-btn--move" @click="$emit('moveQuestionUp', index)">
              上移
            </button>
            <button v-if="index !== questions.length - 1" class="question-list-card__item-action-btn question-list-card__item-action-btn--move" @click="$emit('moveQuestionDown', index)">
              下移
            </button>
            <button class="question-list-card__item-action-btn question-list-card__item-action-btn--edit" @click="$emit('editQuestion', question)">
              编辑
            </button>
            <button class="question-list-card__item-action-btn question-list-card__item-action-btn--delete" @click="$emit('removeQuestion', index)">
              删除
            </button>
          </div>
        </div>

        <div class="question-list-card__item-content">
          <div v-if="question.title" class="question-list-card__item-title">
            {{ question.title }}
          </div>
          <div class="question-list-card__item-description">
            {{ question.description }}
          </div>
          <div v-if="question.type === 0 && question.options.length > 0" class="question-list-card__item-options">
            <div v-for="option in question.options" :key="option.label" class="question-list-card__item-option">
              <span class="question-list-card__item-option-label">{{ option.label }}:</span>
              <span class="question-list-card__item-option-value">{{ option.value }}</span>
            </div>
          </div>
          <div class="question-list-card__item-answer">
            <span class="question-list-card__item-answer-label">正确答案:</span>
            <span class="question-list-card__item-answer-value">{{ question.correctAnswer }}</span>
          </div>
          <div v-if="question.analysis" class="question-list-card__item-analysis">
            <span class="question-list-card__item-analysis-label">解析:</span>
            <span class="question-list-card__item-analysis-value">{{ question.analysis }}</span>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.question-list-card {
  padding: 12px 19px 20px 19px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-bottom: 18px;
}

.question-list-card__header {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
}

.question-list-card__title {
  font-size: 18px;
  font-weight: bold;
  color: #333333;
}

.question-list-card__actions {
  display: flex;
  gap: 8px;
}

.question-list-card__action-btn {
  padding: 6px 19px;
  border-radius: 5px;
  display: flex;
  align-items: center;
  gap: 4px;
  letter-spacing: 0.01em;
  background: linear-gradient(93.74deg, #7a91fe -11.82%, #284af9 116.56%);
  color: white;
  border: none;
  cursor: pointer;
  transition: opacity 0.2s ease;
}

.question-list-card__action-btn:hover {
  opacity: 0.9;
}

.question-list-card__action-btn svg {
  transform: scale(1.2);
  width: 1rem;
  stroke: white;
}

.question-list-card__content {
  display: flex;
  flex-direction: column;
  gap: 16px;
  margin-top: 16px;
}

.question-list-card__empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 40px 20px;
  color: #999;
  background: #f8f9fa;
  border-radius: 8px;
  border: 2px dashed #ddd;
}

.question-list-card__empty-icon {
  font-size: 48px;
  margin-bottom: 12px;
}

.question-list-card__empty-text {
  font-size: 14px;
}

.question-list-card__item {
  background: white;
  border: 1px solid #e9ecef;
  border-radius: 8px;
  padding: 16px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
  transition: box-shadow 0.2s ease;
}

.question-list-card__item:hover {
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

.question-list-card__item-header {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 12px;
  padding-bottom: 8px;
  border-bottom: 1px solid #f0f0f0;
}

.question-list-card__item-number {
  background: var(--primary-color);
  color: white;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 500;
  font-size: 14px;
}

.question-list-card__item-type {
  padding: 4px 12px;
  border-radius: 16px;
  font-size: 12px;
  font-weight: 500;
}

.question-list-card__item-type--choice {
  background: #e3f2fd;
  color: #1976d2;
}

.question-list-card__item-type--essay {
  background: #f3e5f5;
  color: #7b1fa2;
}

.question-list-card__item-actions {
  margin-left: auto;
  display: flex;
  gap: 8px;
}

.question-list-card__item-action-btn {
  padding: 4px 12px;
  border: none;
  border-radius: 4px;
  font-size: 12px;
  cursor: pointer;
  transition: background-color 0.2s ease;
}

.question-list-card__item-action-btn--move {
  color: black;
  box-shadow: 0px 0px 3px rgba(0, 0, 0, 0.2);
  background: transparent;
}

.question-list-card__item-action-btn--edit:hover {
  background: #bbdefb;
}

.question-list-card__item-action-btn--delete {
  background: #ffebee;
  color: #d32f2f;
}

.question-list-card__item-action-btn--delete:hover {
  background: #ffcdd2;
}

.question-list-card__item-content {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.question-list-card__item-title {
  font-weight: 500;
  font-size: 16px;
  color: #333;
}

.question-list-card__item-description {
  color: #555;
  line-height: 1.5;
}

.question-list-card__item-options {
  background: #f8f9fa;
  padding: 12px;
  border-radius: 6px;
  margin: 8px 0;
}

.question-list-card__item-option {
  display: flex;
  gap: 8px;
  margin-bottom: 4px;
}

.question-list-card__item-option:last-child {
  margin-bottom: 0;
}

.question-list-card__item-option-label {
  font-weight: 500;
  color: #666;
  min-width: 24px;
}

.question-list-card__item-option-value {
  color: #333;
}

.question-list-card__item-answer {
  display: flex;
  gap: 8px;
  padding: 8px 12px;
  border-radius: 6px;
}

.question-list-card__item-answer-label {
  font-weight: 500;
  color: var(--primary-color);
}

.question-list-card__item-answer-value {
  color: var(--primary-color-500);
}

.question-list-card__item-analysis {
  display: flex;
  gap: 8px;
  padding: 8px 12px;
  background: #e8f5e8;
  border-radius: 6px;
}

.question-list-card__item-analysis-label {
  font-weight: 500;
  color: #2e7d32;
}

.question-list-card__item-analysis-value {
  color: #2e7d32;
  line-height: 1.4;
}
</style>
